//
//  Array.swift
//  KLExtension
//
//  Created by nkl on 2023/6/29.
//

import Foundation

extension Array {
    
    public func uniqued<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
        var result = [Element]()
        for ele in self {
            let key = filter(ele)
            if !result.map({ filter($0) }).contains(key) {
                result.append(ele)
            }
        }
        return result
    }
    
    public func safeObject(at index: Int) -> Element? {
        if (0 ..< count).contains(index) {
            return self[index]
        } else {
            return nil
        }
    }
}
